package com.gxa.jjty.cjr.controller;

import com.gxa.jjty.cjr.service.EvaluateService;
import com.gxa.jjty.cjr.service.TorderService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.MyEvaluateVo;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EvaluateController
 * @Author: ws
 * @date 2021/12/8 10:06
 */
@RestController
@RequestMapping("/evaluate")
@Api(tags = "评价前台接口")
@Validated
public class EvaluateController {

    @Autowired
    EvaluateService evaluateService;

    @Autowired
    TorderService torderService;

    @PostMapping("/insert")
    @ApiOperation("提交评价操作(生成对应评价的记录)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单的id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "score", value = "评分", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "serverTime", value = "服务时长", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "serverAttitude", value = "服务态度", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "remark", value = "留言、备注", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "imgUrls", value = "图片路径数组", dataType = "String", required = false),
    })
    public Map insert(@ApiIgnore  Evaluate evaluate, String imgUrls) throws IllegalAccessException {

        System.out.println(imgUrls);
        System.out.println(evaluate);

        evaluate.setEvaluateTime(new Timestamp(System.currentTimeMillis()));
        Evaluate insert = evaluateService.insert(evaluate);

        if (!StringUtil.isEmpty(imgUrls)) {

            evaluateService.insertImg(evaluate.getOrderId(), imgUrls);
        }

        // 修改订单表中的flag是否被评论的状态(1: 残疾人自己评论的)
        Torder torder = new Torder();
        torder.setOrderId(evaluate.getOrderId());
        torder.setFlag(1);
        torderService.updateById(torder);

        // 返回前端消息
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(insert);

        return jsonResult.getDataMap();
    }

    @GetMapping("/getMyEvaluate")
    @ApiOperation("通过残疾人id获取待评价信息")
    public Map getMyEvaluate(@RequestParam("cjrId") Long cjrId){

        List<MyEvaluateVo> myEvaluate = evaluateService.getMyEvaluate(cjrId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(myEvaluate);
        jsonResult.put("total",myEvaluate.size());

        return jsonResult.getDataMap();
    }

    @GetMapping("/delete")
    @ApiOperation("根据订单id删除评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id")
    })
    public Map delete(@RequestParam("orderId") Long id)  {

        evaluateService.deleteByOrderId(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

}
